package org.onvivals.admin.module.pms.controller;


import cn.hutool.core.convert.Convert;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.onvivals.common.param.PageParam;
import org.onvivals.common.web.PageResult;
import org.onvivals.common.web.Result;
import org.onvivals.module.pms.entity.PmsTtsConfig;
import org.onvivals.module.pms.param.PmsTtsConfigParam;
import org.onvivals.module.pms.service.IPmsTtsConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "语音合成管理")
@RestController
@RequestMapping("/pms/tts")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class PmsTtsConfigController {

    private final IPmsTtsConfigService pmsTtsConfigService;

	@Operation(summary = "分页查询全部语音合成")
    @GetMapping("/page")
    public Result<PageResult<PmsTtsConfig>> page(PmsTtsConfigParam param) {
        PageParam<PmsTtsConfig, PmsTtsConfigParam> page = new PageParam<>(param);
		page.setDefaultOrder("create_time desc");
        IPage<PmsTtsConfig> pa = pmsTtsConfigService.page(page, page.getWrapper());
        PageResult<PmsTtsConfig> pageResult = new PageResult<>(pa.getRecords(), pa.getTotal());
        return Result.OK("查询成功", pageResult);
    }

    @Operation(summary = "查询全部语音合成")
    @GetMapping()
    public Result<List<PmsTtsConfig>> list(PmsTtsConfigParam param) {
        PageParam<PmsTtsConfig, PmsTtsConfigParam> page = new PageParam<>(param);
        List<PmsTtsConfig> pmsLlmConfigList = pmsTtsConfigService.list(page.getOrderWrapper());
        return Result.OK("查询成功", pmsLlmConfigList);
    }

    @Operation(summary = "根据id查询语音合成")
    @GetMapping("/{id}")
    public Result<PmsTtsConfig> get(@PathVariable("id") Long id) {
        PmsTtsConfig pmsTtsConfig = pmsTtsConfigService.getById(id);
        return Result.OK("查询成功", pmsTtsConfig);
    }

    @Operation(summary = "添加语音合成")
    @PostMapping()
    public Result<?> save(@RequestBody PmsTtsConfig pmsTtsConfig) {
        if (pmsTtsConfigService.save(pmsTtsConfig)) {
            return Result.OK("添加成功");
        }
        return Result.error("添加失败");
    }

    @Operation(summary = "修改语音合成")
    @PutMapping()
    public Result<?> update(@RequestBody PmsTtsConfig pmsTtsConfig) {
        if (pmsTtsConfigService.updateById(pmsTtsConfig)) {
            return Result.OK("修改成功");
        }
        return Result.error("修改失败");
    }

    @Operation(summary = "删除语音合成")
    @DeleteMapping("/{id}")
    public Result<?> remove(@PathVariable("id") Long id) {
        if (pmsTtsConfigService.removeById(id)) {
            return Result.OK("删除成功");
        }
        return Result.error("删除失败");
    }
}
